<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>网络分析服务-选址分区分析</title>
    <script type="text/javascript" src="../../dist/include-openlayers.js"></script>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%">
<div id="map" style="width: 100%;height:100%"></div>
<script type="text/javascript">
    var map, options,
        baseUrl = (window.isLocal ? document.location.protocol + "//" + document.location.host : "http://support.supermap.com.cn:8090")+"/iserver/services/map-changchun/rest/maps/长春市区图",
        serviceUrl = (window.isLocal ? document.location.protocol + "//" + document.location.host : "http://support.supermap.com.cn:8090")+"/iserver/services/transportationanalyst-sample/rest/networkanalyst/RoadNet@Changchun";
    var extent = [48.4, -7668.25, 8958.85, -55.58];
    var projection = new ol.proj.Projection({
        extent: extent,
        units: 'm'
    });
    new ol.supermap.MapService(baseUrl).getMapInfo(function (serviceResult) {
        var mapJSONObj = serviceResult.result;
        map = new ol.Map({
            target: 'map',
            controls: ol.control.defaults({attributionOptions: {collapsed: false}})
                .extend([new ol.supermap.control.Logo()]),
            view: new ol.View({
                center: [4800, -3700],
                zoom: 2,
                projection: projection
            }),
        });
        options = ol.source.TileSuperMapRest.optionsFromMapJSON(baseUrl, mapJSONObj);
        var layer = new ol.layer.Tile({
            source: new ol.source.TileSuperMapRest(options)
        });
        map.addLayer(layer);
        findLocationProcess();
    });

    function findLocationProcess() {
        //添加设施点
        var facilityPoint1 = new ol.geom.Point([1675.9256791377206, -593.56822512495194]);
        var facilityPoint2 = new ol.geom.Point([2820.35101097629, -2358.0414663985171]);
        var facilityPoint3 = new ol.geom.Point([2909.4396668115278, -3647.0074300836109]);
        var facilityPoint4 = new ol.geom.Point([1544.5037476378677, -5616.5950974905827]);
        var facilityPoint5 = new ol.geom.Point([6623.5972101719526, -2130.4887600981415]);
        var facilityPoint6 = new ol.geom.Point([5482.4979617984973, -4504.2328567816048]);
        var facilityPoint7 = new ol.geom.Point([6940.6579024271468, -1627.6012900626256]);
        var facilityPoint8 = new ol.geom.Point([8215.9188781715948, -5747.5063918659716]);
        var facilityFeature1 = new ol.Feature(facilityPoint1);
        var facilityFeature2 = new ol.Feature(facilityPoint2);
        var facilityFeature3 = new ol.Feature(facilityPoint3);
        var facilityFeature4 = new ol.Feature(facilityPoint4);
        var facilityFeature5 = new ol.Feature(facilityPoint5);
        var facilityFeature6 = new ol.Feature(facilityPoint6);
        var facilityFeature7 = new ol.Feature(facilityPoint7);
        var facilityFeature8 = new ol.Feature(facilityPoint8);
        var iconStyle_facility = new ol.style.Style({
            image: new ol.style.Icon(({
                src: '../img/marker.png',
                anchor: [0.5, 1]
            }))
        });
        facilityFeature1.setStyle(iconStyle_facility);
        facilityFeature2.setStyle(iconStyle_facility);
        facilityFeature3.setStyle(iconStyle_facility);
        facilityFeature4.setStyle(iconStyle_facility);
        facilityFeature5.setStyle(iconStyle_facility);
        facilityFeature6.setStyle(iconStyle_facility);
        facilityFeature7.setStyle(iconStyle_facility);
        facilityFeature8.setStyle(iconStyle_facility);
        var facilitySource = new ol.source.Vector({
            features: [
                facilityFeature1, facilityFeature2,
                facilityFeature3, facilityFeature4,
                facilityFeature5, facilityFeature6,
                facilityFeature7, facilityFeature8
            ]
        });
        var facilityLayer = new ol.layer.Vector({
            source: facilitySource
        });

        // 设置设施点的资源供给中心
        var supplyCenterType_FIXEDCENTER = SuperMap.SupplyCenterType.FIXEDCENTER,
            supplyCenterType_NULL = SuperMap.SupplyCenterType.NULL;
        var supplyCenterType_OPTIONALCENTER = SuperMap.SupplyCenterType.OPTIONALCENTER,
            supplyCenters = [new SuperMap.SupplyCenter({
                maxWeight: 500,             // 资源供给中心的最大耗费值,必设参数
                nodeID: 139,                // 资源供给中心点的结点 ID 号,必设参数
                resourceValue: 100,         // 资源供给中心能提供的最大服务量或商品数量,必设参数
                type: supplyCenterType_OPTIONALCENTER      //选址分区中资源中心的类型包括固定中心和可选中心两种
            }),
                new SuperMap.SupplyCenter({
                    maxWeight: 500,
                    nodeID: 1358,
                    resourceValue: 100,
                    type: supplyCenterType_OPTIONALCENTER
                }),
                new SuperMap.SupplyCenter({
                    maxWeight: 500,
                    nodeID: 2972,
                    resourceValue: 100,
                    type: supplyCenterType_OPTIONALCENTER
                }),
                new SuperMap.SupplyCenter({
                    maxWeight: 500,
                    nodeID: 5523,
                    resourceValue: 100,
                    type: supplyCenterType_OPTIONALCENTER
                }),
                new SuperMap.SupplyCenter({
                    maxWeight: 500,
                    nodeID: 1161,
                    resourceValue: 100,
                    type: supplyCenterType_OPTIONALCENTER
                }),
                new SuperMap.SupplyCenter({
                    maxWeight: 500,
                    nodeID: 4337,
                    resourceValue: 100,
                    type: supplyCenterType_OPTIONALCENTER
                }),
                new SuperMap.SupplyCenter({
                    maxWeight: 500,
                    nodeID: 5732,
                    resourceValue: 100,
                    type: supplyCenterType_NULL
                }),
                new SuperMap.SupplyCenter({
                    maxWeight: 500,
                    nodeID: 663,
                    resourceValue: 100,
                    type: supplyCenterType_FIXEDCENTER
                })
            ];

        //创建选址分区分析参数实例
        var findLocationParameter = new SuperMap.FindLocationParameters({
            // 期望用于最终设施选址的资源供给中心数量,必设字段
            expectedSupplyCenterCount: 8,
            // 是否从中心点开始分配资源。默认为 false
            isFromCenter: false,
            nodeDemandField: "Demand",
            // 转向权值字段的名称
            turnWeightField: "TurnCost",
            // 阻力字段的名称, 必设
            weightName: "length",
            // 资源供给中心集合,必设字段
            supplyCenters: supplyCenters
        });

        //进行查找
        new ol.supermap.NetworkAnalystService(serviceUrl).findLocation(findLocationParameter, function (serviceResult) {
            var vectorSource = new ol.source.Vector({
                features: (new ol.format.GeoJSON()).readFeatures(serviceResult.result.demandResults)
            });
            var polygonLayer = new ol.layer.Vector({
                source: vectorSource,
                style: new ol.style.Style({
                    image: new ol.style.Circle({
                        radius: 2.5,
                        fill: new ol.style.Fill({
                            color: 'dodgerblue'
                        })
                    })
                })
            });
            map.addLayer(polygonLayer);
            map.addLayer(facilityLayer);
        });
    }
</script>
</body>
</html>